clear
clear matrix
set matsize 1000
set more off


*this command opens the FARE dataset for the year 2008
use ".........\fare2008.dta", clear

*the following code attaches the correspondence between nace rev 2 codes (ape_08) in FARE and the aggregate industry breakdown from input-output tables based on nace rev 1.1
sort ape_08
merge m:1 ape_08 using ".....\Done2.dta"

*we drop cases that we cannot assign
drop if aggregate_ind==.

*we drop cases with missing turnover or turnover less than 50 thousand euros 
rename caht turnover
drop if turnover==.
drop if turnover<=50

*we now trim the data
egen p99_e=pctile(turnover), p(97.5) 
drop if turnover>=p99_e
drop p99_e



*finally we compute what we need
gen ln_turnover=log(turnover)
egen tot_turn_ind=sum(turnover), by(aggregate_ind)
gen mark_share_2=(turnover/tot_turn_ind)^2
egen double SD_log_r=sd(ln_turnover), by(aggregate_ind)
egen double Mean_log_turnover=mean(ln_turnover), by(aggregate_ind)
gen double dev_mean_4=(ln_turnover-Mean_log_turnover)^4
egen double Mean_dev_mean_4=mean(dev_mean_4), by(aggregate_ind)
gen double Sigma_4=SD_log_r^4

gen n_firms=1
collapse (sum) n_firms mark_share_2 (mean) SD_log_r Sigma_4 Mean_dev_mean_4, by(aggregate_ind)

save "....\bs_data_all_fin_robust_revenue.dta", replace
export excel using "....\bs_data_all_fin_robust_revenue.xls", replace
